package com.tt.aile.upload.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author 86176 shkstrat
 * @date 2020/9/21 - 17:34
 */
@Configuration
public class AileCorsFilterConfiguration {

    //配置cors过滤器
    @Bean
    public CorsFilter corsFilter(){

        //初始化配置对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //设置允许哪些域名可以跨域访问，可以为*代表所有域名都可以跨域访问，但是无法携带cookie
        corsConfiguration.addAllowedOrigin("http://manage.aile.com");

        //设置是否携带cookie跨域访问
        corsConfiguration.setAllowCredentials(true);

        //设置允许哪些请求方式跨域访问
        corsConfiguration.addAllowedMethod("*");

        //设置允许携带哪些头信息跨域访问
        corsConfiguration.addAllowedHeader("*");

        //初始化配置源对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();

        configurationSource.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsFilter(configurationSource);
    }
}
